var prefix = "/market/productAdvertisement"
$(function () {
    load();
});

function load() {
    $('#exampleTable')
        .bootstrapTable(
            {
                method: 'get', // 服务器数据的请求方式 get or post
                url: prefix + "/list", // 服务器数据的加载地址
                //	showRefresh : true,
                //	showToggle : true,
                //	showColumns : true,
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: true, // 设置为true会有隔行变色效果
                dataType: "json", // 服务器返回的数据类型
                pagination: true, // 设置为true会在底部显示分页条
                // queryParamsType : "limit",
                // //设置为limit则会发送符合RESTFull格式的参数
                singleSelect: false, // 设置为true将禁止多选
                // contentType : "application/x-www-form-urlencoded",
                // //发送到服务器的数据编码类型
                pageSize: 10, // 如果设置了分页，每页数据条数
                pageNumber: 1, // 如果设置了分布，首页页码
                //search : true, // 是否显示搜索框
                search: false,//	是否启用搜索框
                showRefresh: true,//是否显示 刷新按钮
                showColumns: true, // 是否显示内容下拉框（选择显示的列）
                sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
                queryParams: function (params) {
                    return {
                        //说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
                        limit: params.limit,
                        offset: params.offset,
                        sort: params.sort,  //排序列名
                        order: params.order,//排位命令（desc，asc）
                        advpositionid: $('#advpositionid').val(),
                        channelid: $('#channelid').val(),
                        advname: $('#advname').val(),
                        advertisername: $('#advertisername').val(),
                    };
                },
                // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
                // queryParamsType = 'limit' ,返回参数必须包含
                // limit, offset, search, sort, order 否则, 需要包含:
                // pageSize, pageNumber, searchText, sortName,
                // sortOrder.
                // 返回false将会终止请求
                columns: [{
                    checkbox: true
                }, {
                    field: 'productname',
                    title: '产品名称'
                },
                    {
                        field: 'advpositionname',
                        title: '广告位名称'
                    },
                    {
                        field: 'channelname',
                        title: '渠道名称'
                    },
                    {
                        field: 'advname',
                        title: '广告名称'
                    },
                    {
                        field: 'advertisername',
                        sortable: true,
                        title: '广告主名称'
                    },
                    {
                        field: 'advevent',
                        title: '广告事件',
                        visible: false
                    },
                    {
                        field: 'advdes',
                        title: '广告描述',
                        visible: false
                    },
                    {
                        field: 'advtype',
                        title: '广告类型',
                        visible: false,
                        formatter: function (value, row, index) {
                            if (value === 1) {
                                return 'JS';
                            } else if (value === 2) {
                                return '素材链接';
                            } else if (value === 3) {
                                return '链接';
                            }
                        }
                    },
                    {
                        field: 'advprice',
                        sortable: true,
                        title: '刊例价格'
                    },
                    {
                        field: 'advsort',
                        sortable: true,
                        title: '排序',
                        visible: false
                    },
                    {
                        field: 'advimgurl',
                        title: '广告图片url',
                        visible: false,
                        formatter: function (value, row, index) {
                            var s = '<a class = "view"  href="javascript:void(0)"><img style="width:70px;height:30px;"  src="' + value + '" /></a>';
                            return s;
                        },
                        events: 'operateEvents'
                    },
                    {
                        field: 'advurl',
                        title: '广告url',
                        visible: false
                    },
                    {
                        field: 'advjs',
                        sortable: true,
                        title: '广告js',
                        visible: false,
                        formatter: function (value, rowData, rowIndex) {
                            if (value != null && value.length >= 1) {
                                return $('<span/>').text(value).html();
                            }
                        }
                    },
                    {
                        field: 'advsettlement',
                        sortable: true,
                        title: '结算方式'
                    },
                    {
                        field: 'isdelete',
                        title: '状态',
                        visible: false,
                        formatter: function (value, row, index) {
                            if (value === 1) {
                                return '正常';
                            } else {
                                return '删除';
                            }
                        }
                    },
                    {
                        field: 'publishstate',
                        title: '发布状态',
                        sortable: true,
                        formatter: function (value, row, index) {
                            if (value === 1) {
                                return '已发布';
                            } else {
                                return '未发布';
                            }
                        }
                    },
                    {
                        field: 'isdirectional',
                        title: '是否定向',
                        formatter: function (value, row, index) {
                            if (value === 1) {
                                return '是';
                            } else {
                                return '否';
                            }
                        }
                    },
                    {
                        field: 'createtime',
                        title: '创建时间',
                        visible: false
                    },
                    {
                        title: '操作',
                        field: 'id',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var aa = '';
                            if (row.isdelete == 0) {
                                aa = '<a class="btn btn-success btn-xs" href="#" mce_href="#" title="点击开启" onclick="remove(\''
                                    + row.id + '\',\'' + row.isdelete + '\',\'' + row.publishstate + '\')"><i class="fa fa-hourglass-start"></i>开启</a> ';
                            } else {
                                aa = '<a class="btn btn-danger btn-xs" href="#" mce_href="#" title="点击删除" onclick="remove(\''
                                    + row.id + '\',\'' + row.isdelete + '\',\'' + row.publishstate + '\')"><i class="fa fa-square-o"></i>删除</a> ';
                            }
                            var bb = '';
                            if (row.publishstate == 0) {
                                bb = '<a class="btn btn-success btn-xs" href="#" mce_href="#" title="点击上架" onclick="removepublishstate(\''
                                    + row.id + '\',\'' + row.publishstate + '\',\'' + row.isdelete + '\')"><i class="fa fa-hourglass-start"></i>上架</a> ';
                            } else {
                                bb = '<a class="btn btn-danger btn-xs" href="#" mce_href="#" title="点击下架" onclick="removepublishstate(\''
                                    + row.id + '\',\'' + row.publishstate + '\',\'' + row.isdelete + '\')"><i class="fa fa-square-o"></i>下架</a> ';
                            }
                            var e = '<a class="btn btn-primary btn-sm ' + s_edit_h + '" href="#" mce_href="#" title="编辑" onclick="edit(\''
                                + row.id
                                + '\')"><i class="fa fa-edit"></i></a> ';
                            var d = '<a class="btn btn-warning btn-sm ' + s_remove_h + '" href="#" title="删除"  mce_href="#" onclick="remove(\''
                                + row.id
                                + '\')"><i class="fa fa-remove"></i></a> ';
                            var f = '<a class="btn btn-success btn-sm" href="#" title="备用"  mce_href="#" onclick="resetPwd(\''
                                + row.id
                                + '\')"><i class="fa fa-key"></i></a> ';
                            if (row.isdelete == 0) {
                                return aa;
                            } else {
                                return e + aa + bb;
                            }
                        }
                    }]
            });
    window.operateEvents = {
        'click .view': function (e, value, row, index) {
            layer.open({
                type: 1,
                title: false,
                closeBtn: 0,
                area: 'auto',
                skin: 'layui-layer-nobg', //没有背景色
                shadeClose: true,
                content: '<img style="width:500px;height:500px;" src="' + value + '"/>'
            });
        },
    };
}

function reLoad() {
    $('#exampleTable').bootstrapTable('refresh');
    $('#exampleTable').bootstrapTable('refreshOptions', {pageNumber: 1});
}

function add() {
    layer.open({
        type: 2,
        title: '增加',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['800px', '460px'],
        content: prefix + '/add?channelid=' + $('#channelid').val() + "&advpositionid=" + $('#advpositionid').val() + "&productid=" + $('#productid').val() // iframe的url
    });
    /*   $.ajax({
           url: prefix + "/JudgingUnique",
           type: "post",
           data: {
               'advpositiontype': $('#advpositiontype').val(),
               'advpositionid': $('#advpositionid').val(),
               'channelid': $('#channelid').val(),
           },
           success: function (r) {
               if (r.code == 0) {
                   layer.open({
                       type: 2,
                       title: '增加',
                       maxmin: true,
                       shadeClose: false, // 点击遮罩关闭层
                       area: ['800px', '520px'],
                       content: prefix + '/add?channelid=' + $('#channelid').val() + "&advpositionid=" + $('#advpositionid').val() + "&productid=" + $('#productid').val() // iframe的url
                   });
               } else {
                   layer.alert("通用广告位只能存在一条有效广告");
               }
           }
       });*/
}

function edit(id) {
    layer.open({
        type: 2,
        title: '编辑',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['800px', '460px'],
        content: prefix + '/edit/' + id // iframe的url
    });
}

function remove(id, isdelete, publishstate) {
    var actCh;
    if (isdelete == 0) {
        isdelete = 1;
        actCh = "确认要开启广告吗？";
    } else {
        isdelete = 0;
        actCh = "确认要删除广告吗？";
    }
    layer.confirm(actCh, {
        btn: ['确定', '取消']
    }, function () {
        if (isdelete == 1 && publishstate == 1) {
            $.ajax({
                url: prefix + "/JudgingUnique",
                type: "post",
                data: {
                    'advpositiontype': $('#advpositiontype').val(),
                    'advpositionid': $('#advpositionid').val(),
                    'channelid': $('#channelid').val(),
                },
                success: function (r) {
                    if (r.code == 0) {
                        $.ajax({
                            url: prefix + "/remove",
                            type: "post",
                            data: {
                                'id': id,
                                'isdelete': isdelete
                            },
                            success: function (r) {
                                if (r.code == 0) {
                                    layer.msg(r.msg);
                                    reLoad();
                                } else {
                                    layer.msg(r.msg);
                                }
                            }
                        });
                    } else {
                        layer.alert("通用广告位只能存在一条有效广告");
                    }
                }
            });
        } else {
            $.ajax({
                url: prefix + "/remove",
                type: "post",
                data: {
                    'id': id,
                    'isdelete': isdelete
                },
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        reLoad();
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        }

    })
}

function removepublishstate(id, publishstate, isdelete) {
    var actCh;
    if (publishstate == 0) {
        publishstate = 1;
        actCh = "确认要上架广告吗？";
    } else {
        publishstate = 0;
        actCh = "确认要下架广告吗？";
    }
    layer.confirm(actCh, {
        btn: ['确定', '取消']
    }, function () {
        if (publishstate == 1 && isdelete == 1) {
            $.ajax({
                url: prefix + "/JudgingUnique",
                type: "post",
                data: {
                    'advpositiontype': $('#advpositiontype').val(),
                    'advpositionid': $('#advpositionid').val(),
                    'channelid': $('#channelid').val(),
                },
                success: function (r) {
                    if (r.code == 0) {
                        $.ajax({
                            url: prefix + "/remove",
                            type: "post",
                            data: {
                                'id': id,
                                'publishstate': publishstate
                            },
                            success: function (r) {
                                if (r.code == 0) {
                                    layer.msg(r.msg);
                                    reLoad();
                                } else {
                                    layer.msg(r.msg);
                                }
                            }
                        });
                    } else {
                        layer.alert("通用广告位只能存在一条有效广告");
                    }
                }
            });
        } else {
            $.ajax({
                url: prefix + "/remove",
                type: "post",
                data: {
                    'id': id,
                    'publishstate': publishstate
                },
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        reLoad();
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        }
    })
}

function resetPwd(id) {
}

function batchRemove() {
    var advpositiontype = $('#advpositiontype').val();
    if (advpositiontype == '') {
        layer.alert("通用广告位不适用批量上架");
    } else {
        var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
        if (rows.length == 0) {
            layer.msg("请选择要上架的数据");
            return;
        }
        layer.confirm("确认要上架选中的'" + rows.length + "'条数据吗?", {
            btn: ['确定', '取消']
            // 按钮
        }, function () {
            var ids = new Array();
            // 遍历所有选择的行数据，取每条数据对应的ID
            $.each(rows, function (i, row) {
                ids[i] = row['id'];
            });
            $.ajax({
                type: 'POST',
                data: {
                    "ids": ids
                },
                url: prefix + '/batchRemove',
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        reLoad();
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        }, function () {

        });
    }
}